﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sami_SMS_CRM_Business;
using System.Web.Services;

namespace Sami_SMS_CRM_UI
{
    public partial class crm_subscriber_classification : BasePage
    {
        SubscriberClassificationRepository repository = new SubscriberClassificationRepository();
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            pagingtop.PagingChange += new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange += new EventHandler(paging_PagingChange);
        }

        void paging_PagingChange(object sender, EventArgs e)
        {
            pagingtop.PagingChange -= new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange -= new EventHandler(paging_PagingChange);
            pagingtop.CurrentPage = (sender as UserControl.ucPaging).CurrentPage;
            pagingbottom.CurrentPage = (sender as UserControl.ucPaging).CurrentPage;
            BindDataTable();
            pagingtop.Visible = (sender as UserControl.ucPaging).TotalPage > 1;
            pagingtop.PagingChange += new EventHandler(paging_PagingChange);
            pagingbottom.Visible = (sender as UserControl.ucPaging).TotalPage > 1;
            pagingbottom.PagingChange += new EventHandler(paging_PagingChange);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnDeleteAll.Visible = false;
                PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
                pagingtop.CurrentPage = 1;
                pagingtop.PageSize = PageSize;
                pagingtop.PageDisplay = PageDisPlay;
                pagingbottom.CurrentPage = 1;
                pagingbottom.PageSize = PageSize;
                pagingbottom.PageDisplay = PageDisPlay;
                BindDataTable();
            }
        }

        private void BindDataTable()
        {
            try
            {
                int currentPage = pagingtop.CurrentPage;
                int pageSize = pagingtop.PageSize;
                int totalRecord = 0;
                string subscriber = txtSubscriber.Text.Trim();
                DateTime? timeFrom = string.IsNullOrEmpty(txtFrom.Text) ?
                                        (DateTime?)null : Convert.ToDateTime(txtFrom.Text.Trim(), DateTimeFormat);
                DateTime? timeTo = string.IsNullOrEmpty(txtTo.Text) ?
                                        (DateTime?)null : Convert.ToDateTime(txtTo.Text.Trim(), DateTimeFormat);
                rptTable.DataSource = repository.GetAll(subscriber, timeFrom, timeTo, currentPage, pageSize, ref totalRecord);
                rptTable.DataBind();
                pagingtop.TotalRecord = totalRecord;
                pagingtop.Visible = pagingtop.TotalPage > 1;
                pagingbottom.TotalRecord = totalRecord;
                pagingbottom.Visible = pagingtop.TotalPage > 1;
                ltrTotalRow.Text = totalRecord.ToString();
            }
            catch (Exception e)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('" + Server.HtmlEncode(e.Message) + "');", true);
            }
        }

        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            pagingtop.PageSize = pagingbottom.PageSize = int.Parse((sender as DropDownList).SelectedValue);
            BindDataTable();
        }

        protected void btnDeleteAll_Click(object sender, ImageClickEventArgs e)
        {
            if (repository.DeleteRecordExpired() > 0)
                BindDataTable();
        }

        protected void rptTable_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            try
            {
                if (IsAdmin())
                {
                    Guid id = Guid.Parse(e.CommandArgument.ToString());
                    switch (e.CommandName)
                    {
                        case "Edited":
                            var entity = repository.GetById(id);
                            ScriptManager.RegisterStartupScript(this, this.GetType(), "", "dialog();", true);
                            break;
                        case "Deleted":
                            repository.DeleteById(id);
                            BindDataTable();
                            break;
                        case "ViewDetail":
                            break;
                    }
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert(\"You don't have permission!\");", true);
                }
            }
            catch (Exception ex)
            {
                new LogRepository().InsertLog(ex);
            }
        }

        protected void btnSearching_Click(object sender, EventArgs e)
        {
            BindDataTable();
        }
    }
}